package com.example.chenhuamou.freenews;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.example.chenhuamou.freenews.view.discover.DiscoverFragment;
import com.example.chenhuamou.freenews.view.mine.MineFragment;
import com.example.chenhuamou.freenews.view.news.NewsFragment;
import com.example.chenhuamou.freenews.view.video.VideoFragment;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {

    @BindView(R.id.btn_news)
    Button btn_news;

    @BindView(R.id.btn_video)
    Button btn_video;

    @BindView(R.id.btn_discover)
    Button btn_discover;

    @BindView(R.id.btn_mine)
    Button btn_mine;

    @BindView(R.id.viewPager)
    ViewPager viewPager;

    private Context mContext;

    // tab bar 默认选中值
    public int mSelectIndex = 0;

    // fragment 数组
    private ArrayList<android.support.v4.app.Fragment> mFragmentList;

    /*
    * activity 生命周期
    * */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 绑定 Butter Knife
        ButterKnife.bind(this);
        mContext = this;

        // 设置 tab bar 选中状态
        setTabBarSelectedIndex(mSelectIndex);

        // 设置 adapter
        viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
        // 设置页面改变监听
        viewPager.addOnPageChangeListener(this);

    }

    /*
        * 点击 tab bar 按钮
        * */
    @OnClick({R.id.btn_news, R.id.btn_video, R.id.btn_discover, R.id.btn_mine})
    public void onClick(View view) {
        // 把所有按钮设置为非选中状态
        resetTabBarSelectState();
        int index = 0;
        switch (view.getId()) {
            case R.id.btn_news:
                index = 0;
                break;
            case R.id.btn_video:
                index = 1;
                break;
            case R.id.btn_discover:
                index = 2;
                break;
            case R.id.btn_mine:
                index = 3;
                break;
            default:
                break;
        }
        // 根据 ID 来设置 tab bar 按钮的选中状态
        setTabBarSelectedIndex(index);
        // viewPager 滑动到对应的 fragment 页面，关闭滑动动画效果
        viewPager.setCurrentItem(index, false);
    }

    /*
    * 把 tab bar 的按钮全部设置为非选中状态
    * */
    private void resetTabBarSelectState() {
        btn_news.setSelected(false);
        btn_video.setSelected(false);
        btn_discover.setSelected(false);
        btn_mine.setSelected(false);
    }

    /*
    * 根据 ID 来设置 tab bar 按钮的选中状态
    * */
    private void setTabBarSelectedIndex(int index) {
        // 根据ID来设置选中
        switch (index) {
            case 0:
                btn_news.setSelected(true);
                break;
            case 1:
                btn_video.setSelected(true);
                break;
            case 2:
                btn_discover.setSelected(true);
                break;
            case 3:
                btn_mine.setSelected(true);
                break;
            default:
                btn_news.setSelected(true);
                break;
        }
    }

    /*
    * viewPager 页面改变监听
    * */
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { }

    @Override
    public void onPageSelected(int position) {
        resetTabBarSelectState();
        setTabBarSelectedIndex(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) { }

    /*
    * viewPager 的 adapter
    * */
    private class ViewPagerAdapter extends FragmentPagerAdapter {

        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        /**
         * Return the Fragment associated with a specified position.
         *
         * @param position
         */
        @Override
        public Fragment getItem(int position) {
            Fragment fragment = null;
            switch (position) {
                case 0:
                    fragment = new NewsFragment();
                    break;
                case 1:
                    fragment = new VideoFragment();
                    break;
                case 2:
                    fragment = new DiscoverFragment();
                    break;
                case 3:
                    fragment = new MineFragment();
                    break;
                default:
                    break;
            }
            return fragment;
        }

        /**
         * Return the number of views available.
         */
        @Override
        public int getCount() {
            return 4;
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        System.out.println("新浪微博分享回调来这里了----------------------");
    }
}
